<?php

class bookmarks_IndexController extends Zend_Controller_Action
{
  function init() {
	$this->view->headLink()->appendStylesheet($this->view->baseUrl().'/ressources/bookmarks/style.css');

	//for the home we may return the response in JSOn format	
	$contextSwitch = $this->_helper->getHelper('contextSwitch');
	$contextSwitch->addActionContext('index', 'json');	
	$contextSwitch->initContext();
	

  }

  function postDispatch() {
	
	$this->saveSidebarLinks();
	$this->saveBreadcrumbs();
  }

  //we display the home of the authenticated user
  function indexAction() {  
  	
	$this->view->mode = 'home'; 

	//We get the home data of the user
	$engine = Myzen_Oauth_Query_Singleton::getInstance();    
    
	$params = array();
	$beforeBookmarkId = $this->_getParam('before');
	
	if(!empty($beforebookmarkId)) $params['beforeBookmarkId'] = $beforeBookmarkId;
	
	$data = $engine->query('bookmarks','list','user',$params);	

	$stream = unserialize($data);
	$this->view->stream = $stream;

	//If we are in JSONMode, we just need to format the bookmarkes and send them back
	if($this->_helper->getHelper('contextSwitch')->getCurrentContext() == 'json') {

		
		$bookmarks = @$stream['bookmarks'];		
		
		
		if(empty($bookmarks)) {
			$this->view->nbBookmarks= 0;
		}
		else {
			$this->view->nbBookmarks= count($bookmarks);
			$htmlString = '';
			$userId = Zend_Registry::get('app_session')->userDetails['id'];
			foreach($bookmarks as $mun=> $bookmark) {

				$htmlString .= $this->view->partial('partialbookmark.phtml', array(
					'bookmark' => $bookmark,
					'userId' => $userId 
					)
				);
			} //end foreach bookmark
			$this->view->htmlString = $htmlString;
		} //end bookmarkes not empty
		return;
	} //end json mode	

 	$this->_helper->viewRenderer('bookmarkslist');
	
	$this->view->userId = Zend_Registry::get('app_session')->userDetails['id'];
	
	//Add javascript for infinite scrolling
	$this->view->headScript()->appendFile($this->view->baseUrl().'/ressources/bookmark/javascript/infiniteScrolling.js', 'text/javascript');
	
	$this->view->YUILoader()->load(array('yahoo', 'dom', 'event','connection','json'));
	$this->view->javascriptData('infiniteScrollBaseUrl',$this->view->baseURL().'/bookmark/index/index/format/json/before/'); 


	
	return;
  }

  //we display the home of the authenticated user
  function publicAction() {      
	//We get the home data of the user
	$engine = Myzen_Oauth_Query_Singleton::getInstance();    
    
	$params = array();
	$beforebookmarkId = $this->_getParam('before');
	
	if(!empty($beforebookmarkId)) $params['beforebookmarkId'] = $beforebookmarkId;
	
	$data = $engine->query('bookmarks','list','public',$params);
	$stream = unserialize($data);
	$this->view->stream = $stream;
	
	//If we are in JSONMode, we just need to format the bookmarkes and send them back
	if($this->_helper->getHelper('contextSwitch')->getCurrentContext() == 'json') {
		$bookmarks = @$stream['data'];		
		if(empty($bookmarks)) {
			$this->view->nbBookmarks= 0;
		}
		else {
			$this->view->nbBookmarks= count($bookmarks);
			$htmlString = '';
			$userId = Zend_Registry::get('app_session')->userDetails['id'];
			foreach($bookmarks as $mun=> $bookmark) {

				$htmlString .= $this->view->partial('partialbookmark.phtml', array(
					'bookmark' => $bookmark,
					'avatarBaseURL' => $this->view->avatarBaseURL,
					'userId' => $userId 
					)
				);
			} //end foreach bookmark
			$this->view->htmlString = $htmlString;
		} //end bookmarkes not empty
		return;
	} //end json mode

	
	$this->view->mode = 'public';
 	$this->_helper->viewRenderer('bookmarkslist');
	$this->view->userId = Zend_Registry::get('app_session')->userDetails['id'];
		
	//Add javascript for infinite scrolling
	$this->view->headScript()->appendFile($this->view->baseUrl().'/ressources/bookmark/javascript/infiniteScrolling.js', 'text/javascript');
	
	$this->view->YUILoader()->load(array('yahoo', 'dom', 'event','connection','json'));
	$this->view->javascriptData('infiniteScrollBaseUrl',$this->view->baseURL().'/bookmark/index/index/format/json/before/'); 

	
	

	
  } //end IndexAction


  
  
  
  
  
  
  
  //we display the home of the authenticated user
  function userAction() {      
	
	//If the user is displaying his own user page, we redirect him to his home
	if(Zend_Registry::get('app_session')->userDetails['pseudo'] == $this->_getParam('pseudo')){
		$this->_forward('index');
		return;
	}
	
	//We get the home data of the user
	$engine = Myzen_Oauth_Query_Singleton::getInstance();    
    
	$params = array();
	$params['userPseudo']= $this->_getParam('pseudo');

	
	if(!empty($beforebookmarkId)) $params['beforebookmarkId'] = $beforebookmarkId;
	
	$data = $engine->query('bookmarks','list','user',$params);
	$stream = unserialize($data);
	$this->view->stream = $stream;
	
	//If we are in JSONMode, we just need to format the bookmarkes and send them back
	if($this->_helper->getHelper('contextSwitch')->getCurrentContext() == 'json') {
		$bookmarks = @$stream['data'];		
		if(empty($bookmarks)) {
			$this->view->nbBookmarks= 0;
		}
		else {
			$this->view->nbBookmarks= count($bookmarks);
			$htmlString = '';
			$userId = Zend_Registry::get('app_session')->userDetails['id'];
			foreach($bookmarks as $mun=> $bookmark) {

				$htmlString .= $this->view->partial('partialbookmark.phtml', array(
					'bookmark' => $bookmark,
					'avatarBaseURL' => $this->view->avatarBaseURL,
					'userId' => $userId 
					)
				);
			} //end foreach bookmark
			$this->view->htmlString = $htmlString;
		} //end bookmarkes not empty
		return;
	} //end json mode

	
	$this->view->mode = 'user';
	$this->view->userDisplayed = $this->_getParam('pseudo');
 	$this->_helper->viewRenderer('bookmarkslist');
	$this->view->userId = Zend_Registry::get('app_session')->userDetails['id'];
		
	//Add javascript for infinite scrolling
	$this->view->headScript()->appendFile($this->view->baseUrl().'/ressources/bookmark/javascript/infiniteScrolling.js', 'text/javascript');
	
	$this->view->YUILoader()->load(array('yahoo', 'dom', 'event','connection','json'));
	$this->view->javascriptData('infiniteScrollBaseUrl',$this->view->baseURL().'/bookmark/index/index/format/json/before/'); 
  } //end IndexAction
  
  
  
  
  
  
 
  //we display the home of the authenticated user
  function tagAction() {      

	
	//We get the home data of the user
	$engine = Myzen_Oauth_Query_Singleton::getInstance();    
    
	$params = array();
	$params['tagId']= $this->_getParam('tag');

	
	if(!empty($beforebookmarkId)) $params['beforebookmarkId'] = $beforebookmarkId;
	
	$data = $engine->query('bookmarks','list','tag',$params);
	$stream = unserialize($data);
	$this->view->stream = $stream;
	
	//If we are in JSONMode, we just need to format the bookmarkes and send them back
	if($this->_helper->getHelper('contextSwitch')->getCurrentContext() == 'json') {
		$bookmarks = @$stream['data'];		
		if(empty($bookmarks)) {
			$this->view->nbBookmarks= 0;
		}
		else {
			$this->view->nbBookmarks= count($bookmarks);
			$htmlString = '';
			$userId = Zend_Registry::get('app_session')->userDetails['id'];
			foreach($bookmarks as $mun=> $bookmark) {

				$htmlString .= $this->view->partial('partialbookmark.phtml', array(
					'bookmark' => $bookmark,
					'avatarBaseURL' => $this->view->avatarBaseURL,
					'userId' => $userId 
					)
				);
			} //end foreach bookmark
			$this->view->htmlString = $htmlString;
		} //end bookmarkes not empty
		return;
	} //end json mode

	
	$this->view->mode = 'tag';
	$this->view->tagDisplayed = $this->_getParam('tag');
 	$this->_helper->viewRenderer('bookmarkslist');
	$this->view->userId = Zend_Registry::get('app_session')->userDetails['id'];
		
	//Add javascript for infinite scrolling
	$this->view->headScript()->appendFile($this->view->baseUrl().'/ressources/bookmark/javascript/infiniteScrolling.js', 'text/javascript');
	
	$this->view->YUILoader()->load(array('yahoo', 'dom', 'event','connection','json'));
	$this->view->javascriptData('infiniteScrollBaseUrl',$this->view->baseURL().'/bookmark/index/index/format/json/before/'); 
  } //end IndexAction 
  
  
  
  
  
  
  
  
  //we display the home of the authenticated user
  function toolsAction() { 
  	$this->view->mode = 'tools';
  }

	
  function publishAction() {
	//We get the home data of the user
	$engine = Myzen_Oauth_Query_Singleton::getInstance();    
    
	$params = array();
	$params['URL'] = $this->_getParam('URL');
	$params['title'] = $this->_getParam('title');
	$params['tags'] = $this->_getParam('tags');
	$params['description'] = $this->_getParam('description');
	
	if($this->_getParam('private') == 'on') 	$params['isPrivate'] = true;
	else $params['isPrivate'] = false;
	
	if(!empty($beforebookmarkId)) $params['beforebookmarkId'] = $beforebookmarkId;
	
	$data = $engine->query('bookmarks','bookmark','create',$params);
	
	$message = unserialize($data);
	
	if($message['status'] == 'KO') {
	       $this->view->messageDisplay()->setMessage($message['error']['message'],'error');	
		$this->view->URL = 	htmlspecialchars($params['URL']);
		$this->view->title = 	htmlspecialchars($params['title']);
		$this->view->tags = 	htmlspecialchars($params['tags']);
		$this->view->description = 	htmlspecialchars($params['description']);
		$this->view->private = 	$params['isPrivate'];
		$this->_forward('tools');
		return;
	} else {
	       $this->view->messageDisplay()->setMessage('Bookmark has been saved','confirmation');	
	       $this->_forward('index');
	       return;	
	}
	
	
	
	  		
  }


  function deleteAction() {
	//We get the home data of the user
	$engine = Myzen_Oauth_Query_Singleton::getInstance();    
   	
   	$params= array('id' => $this->_getParam('id'));
	
	$data = $engine->query('bookmarks','bookmark','delete',$params);
	
	$message = unserialize($data);
	
	if($message['status'] == 'KO') {
	    $this->view->messageDisplay()->setMessage($message['error']['message'],'error');	
		$this->_forward('index');
		return;
	} else {
	       $this->view->messageDisplay()->setMessage('Bookmark has been deleted','confirmation');	
	       $this->_forward('index');
	       return;	
	}
	
	
	
	  		
  }

  function saveSidebarLinks() {

	$num = 0;

	$userPseudo = Zend_Registry::get('app_session')->userDetails['pseudo'];

	
	
	$links[$num] = array('text' => 'Public', 'url' => '/bookmarks/public/');
	if($this->view->mode == 'public') $links[$num]['selected'] = 1;			
	$num++;

	
	$links[$num] = array('text' => $userPseudo, 'url' => '/bookmarks/');
	if($this->view->mode == 'home') $links[$num]['selected'] = 1;	
	$num++;

	if($this->view->mode == 'user') {
		$links[$num] = array('text' => $this->view->userDisplayed, 'url' => '/bookmarks/user/'.$this->view->userDisplayed.'/');
		$links[$num]['selected'] = 1;	
		$num++;
	}

	if($this->view->mode == 'tag') {
		$links[$num] = array('text' => $this->view->tagDisplayed, 'url' => '/bookmarks/tag/'.$this->view->tagDisplayed.'/');
		$links[$num]['selected'] = 1;	
		$num++;
	}

	
	$links[$num] = array('text' => 'Tools', 'url' => '/bookmarks/tools/');
	if($this->view->mode == 'tools') $links[$num]['selected'] = 1;	
	$num++;	
			
	$this->view->sidebarLinks($links);	
  }
  
  
  
  function saveBreadcrumbs() {
  	$crumbs = array(
		0 => array(
			'text' => 'Bookmarks', 
			'url'  => '/bookmarks/'
			));
	
	if($this->view->mode == 'public') $crumbs[1] = array('text'=> 'public'); 	
	if($this->view->mode == 'home') $crumbs[1] = array('text'=> 'home');
	
	$this->view->Breadcrumbs($crumbs);
	
		
  }




} //End class